home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Source.bin / ImageViewer.java < prev    next >
Text File  |  1998-10-23  |  15KB  |  571 lines

  1. package symantec.itools.multimedia;
  2.  
  3. import java.awt.Component;
  4. import java.awt.Dimension;
  5. import java.awt.Graphics;
  6. import java.awt.Image;
  7. import java.awt.MediaTracker;
  8. import java.awt.Toolkit;
  9. import java.net.URL;
  10. import java.net.MalformedURLException;
  11. import java.beans.PropertyVetoException;
  12. import java.beans.PropertyChangeListener;
  13. import java.beans.VetoableChangeListener;
  14. import java.io.ObjectInputStream;
  15. import java.io.IOException;
  16. import java.util.ResourceBundle;
  17. import java.text.MessageFormat;
  18.  
  19. //    01/29/97    TWB    Integrated changes from Windows
  20. //    02/19/97    RKM    Changed default for centerMode to correspond with DES file
  21. //                    Changed setCenterMode to call repaint instead of invalidate
  22. //                    Changed setCenterMode to repaint only if value had changed
  23. //
  24. //    05/29/97    MSH    Updated to support Java 1.1
  25. //  07/16/97    CAR marked fields transient as needed
  26. //                  implemented readObject to load an Image after deserialization
  27. //  07/17/97    CAR added add/remove property/vetoable ChangeListner methods
  28. //  08/04/97    LAB Made lightweight.  Implemented setStyle and getStyle.  Deprecated
  29. //                    setCentered and getCentered.
  30. //  08/27/97    CAR update setURL and setFileName to "erase" image if URL or String is null
  31. //  08/04/97    LAB Deprecated URL property and replaced it with ImageURL property (Addresses
  32. //                    Mac Bug #7689).  Changed names of strings in PropertyChangeEvent handling
  33. //                    to follow Bean Spec naming conventions.
  34.  
  35. /**
  36.  * ImageViewer component. Provides a platform-independent display of an Image.
  37.  * @version 1.1, August 4, 1997
  38.  * @author    Symantec
  39.  */
  40. public class ImageViewer extends Component implements java.io.Serializable
  41. {
  42.     /**
  43.      * A constant indicating the image is to be tiled in the size of this component.
  44.      */
  45.     public static final int IMAGE_TILED = 0;
  46.     /**
  47.      * A constant indicating the image is to be centered in the size of this component.
  48.      */
  49.     public static final int IMAGE_CENTERED = 1;
  50.     /**
  51.      * A constant indicating the image is to be scaled to fit the size of this component.
  52.      */
  53.     public static final int IMAGE_SCALED_TO_FIT = 2;
  54.     /**
  55.      * A constant indicating the image is to be drawn normally in the upper left corner.
  56.      */
  57.     public static final int IMAGE_NORMAL = 3;
  58.  
  59.     /**
  60.      * Create default image viewer.
  61.      */
  62.     public ImageViewer()
  63.     {
  64.         image        = null;
  65.         fileName    = null;
  66.         url            = null;
  67.         imageStyle    = IMAGE_CENTERED;
  68.     }
  69.  
  70.     /**
  71.      * Crate image viewer with filename.  The specified filename is used as
  72.      * the image source.
  73.      *
  74.      * @param str name of file containing the image source
  75.      * @exception MalformedURLException Thrown if URL cannot be generated from filename
  76.      *
  77.      */
  78.     public ImageViewer(String str) throws MalformedURLException
  79.     {
  80.         this();
  81.  
  82.         try
  83.         {
  84.             setFileName(str);
  85.         }
  86.         catch(PropertyVetoException e){}
  87.     }
  88.  
  89.     /**
  90.      * Create image viewer with URL.  The specified URL is used as
  91.      * the image source.
  92.      *
  93.      * @param url the URL of the image to be displayed
  94.      */
  95.     public ImageViewer(URL url)
  96.     {
  97.         this();
  98.  
  99.         try
  100.         {
  101.              setURL(url);
  102.         }
  103.         catch ( PropertyVetoException e ) { }
  104.     }
  105.  
  106.     /**
  107.      * Create image viewer with image.  The specified image is used as
  108.      * the image source
  109.      *
  110.      * @param img the image to be displayed
  111.      */
  112.     public ImageViewer(Image img)
  113.     {
  114.         this();
  115.  
  116.         try
  117.         {
  118.             setImage(img);
  119.         }
  120.         catch ( PropertyVetoException e ) { }
  121.     }
  122.  
  123.     /**
  124.      * Specify or change the image filename.
  125.      *
  126.      * @param str name of file containing image source
  127.      *
  128.      * @exception PropertyVetoException
  129.      * if the specified property value is unacceptable
  130.      */
  131.     public void setFileName(String str) throws PropertyVetoException
  132.     {
  133.         String oldValue = fileName;
  134.  
  135.         try
  136.         {
  137.             vetos.fireVetoableChange("fileName", oldValue, str );
  138.  
  139.            fileName = str;
  140.            if(fileName != null && fileName != "")
  141.                setURL(new URL(fileName));
  142.            else
  143.                setURL(null);
  144.  
  145.         }
  146.         catch(MalformedURLException e)
  147.         {
  148.             //System.out.println("malformed URL");
  149.             fileName = oldValue;
  150.         }
  151.         repaint();
  152.  
  153.         changes.firePropertyChange("fileName", oldValue, str );
  154.     }
  155.  
  156.     /**
  157.      * Obtain the filename associated with the current image.
  158.      *
  159.      * @return the name of the file, if any, associated with this image.  If
  160.      * no file is associated with this image, returns null
  161.      */
  162.     public String getFileName()
  163.     {
  164.         return fileName;
  165.     }
  166.  
  167.     /**
  168.      * Specify or change the image URL.
  169.      *
  170.      * @param aUrl the URL of the image to be displayed
  171.      *
  172.      * @exception PropertyVetoException
  173.      * if the specified property value is unacceptable
  174.      */
  175.     public void setImageURL(URL aUrl) throws PropertyVetoException
  176.     {
  177.         URL oldValue = url;
  178.         vetos.fireVetoableChange("imageURL", oldValue, aUrl );
  179.  
  180.         url = aUrl;
  181.         fileName = null;
  182.         Image loadedImage = null;
  183.         if(url != null){
  184.             loadedImage = getToolkit().getImage(url);
  185.         }
  186.         setImage(loadedImage);
  187.         repaint();
  188.         changes.firePropertyChange("imageURL", oldValue, aUrl );
  189.     }
  190.  
  191.    /**
  192.      * Obtain the URL associated with the current image.  If the image
  193.      * was specified by file name, or URL, it will have a URL which
  194.      * indicates its source.  Images created using the constructor
  195.      * with an Image parameter will have no associated URL.
  196.      *
  197.      * @return the name of the URL, if any, associated with this image.  If
  198.                no URL is associated with this image, returns null
  199.      */
  200.     public URL getImageURL()
  201.     {
  202.         return url;
  203.     }
  204.  
  205.     /**
  206.      * @deprecated
  207.      * @see #setImageURL
  208.      * @exception PropertyVetoException
  209.      * if the specified property value is unacceptable
  210.      */
  211.     public void setURL(URL aUrl) throws PropertyVetoException
  212.     {
  213.         setImageURL(aUrl);
  214.     }
  215.  
  216.    /**
  217.      * @deprecated
  218.      * @see #getImageURL
  219.      */
  220.     public URL getURL()
  221.     {
  222.         return getImageURL();
  223.     }
  224.  
  225.     /**
  226.      * @deprecated
  227.      * @see #setStyle
  228.      * @exception PropertyVetoException
  229.      * if the specified property value is unacceptable
  230.      */
  231.     public void setCenterMode(boolean flag) throws PropertyVetoException
  232.     {
  233.         if(flag)
  234.         {
  235.             if(getStyle() != IMAGE_CENTERED)
  236.                 setStyle(IMAGE_CENTERED);
  237.         }
  238.         else
  239.         {
  240.             if(getStyle() != IMAGE_NORMAL)
  241.                 setStyle(IMAGE_NORMAL);
  242.         }
  243.     }
  244.  
  245.     /**
  246.      * @deprecated
  247.      * @see #getStyle
  248.      */
  249.     public boolean getCenterMode()
  250.     {
  251.         return (getStyle() == IMAGE_CENTERED);
  252.     }
  253.  
  254.     /**
  255.      * Sets the new panel image style.
  256.      * @param newStyle the new panel image style, one of
  257.      * IMAGE_TILED, IMAGE_CENTERED, or IMAGE_SCALED_TO_FIT
  258.      * @exception PropertyVetoException
  259.      * if the specified property value is unacceptable
  260.      * @see #getStyle
  261.      * @see #IMAGE_TILED
  262.      * @see #IMAGE_CENTERED
  263.      * @see #IMAGE_SCALED_TO_FIT
  264.      * @see #IMAGE_NORMAL
  265.      */
  266.     public void setStyle(int newStyle) throws PropertyVetoException
  267.     {
  268.         if (newStyle != imageStyle)
  269.         {
  270.             Integer oldValue = new Integer(imageStyle);
  271.             Integer newValue = new Integer(newStyle);
  272.  
  273.             vetos.fireVetoableChange("style", oldValue, newValue);
  274.  
  275.             imageStyle = newStyle;
  276.             repaint();
  277.  
  278.             changes.firePropertyChange("style", oldValue, newValue);
  279.         }
  280.     }
  281.  
  282.     /**
  283.      * Gets the current panel image style.
  284.      * @return the current panel image style, one of
  285.      * IMAGE_TILED, IMAGE_CENTERED, or IMAGE_SCALED_TO_FIT
  286.      * @see #setStyle
  287.      * @see #IMAGE_TILED
  288.      * @see #IMAGE_CENTERED
  289.      * @see #IMAGE_SCALED_TO_FIT
  290.      */
  291.     public int getStyle()
  292.     {
  293.         return imageStyle;
  294.     }
  295.  
  296.     /**
  297.      * Set or change the current image.  Call this method if you want to
  298.      * specify directly the image to display.
  299.      *
  300.      * @param img the image to be displayed
  301.      *
  302.      * @exception PropertyVetoException
  303.      * if the specified property value is unacceptable
  304.      */
  305.     public void setImage(Image img) throws PropertyVetoException
  306.     {
  307.         fileName = null;
  308.         Image oldValue = image;
  309.  
  310.         vetos.fireVetoableChange( "image", oldValue, img );
  311.  
  312.         if(image != null)
  313.         {
  314.             image.flush();
  315.             image = null;
  316.         }
  317.         image    = img;
  318.  
  319.         if (img != null)
  320.         {
  321.             MediaTracker tracker;
  322.  
  323.             try
  324.             {
  325.                 tracker = new MediaTracker(this);
  326.                 tracker.addImage(image, 0);
  327.                 tracker.waitForID(0);
  328.             }
  329.             catch(InterruptedException e) { }
  330.         }
  331.         else
  332.               repaint();
  333.  
  334.         changes.firePropertyChange( "image", oldValue, img );
  335.     }
  336.  
  337.     /**
  338.      * Obtain the image currently being displayed.
  339.      *
  340.      * @return the image currently displayed or null if no image
  341.      */
  342.     public Image getImage()
  343.     {
  344.         return image;
  345.     }
  346.  
  347.     /**
  348.      * Paints this component using the given graphics context.
  349.      * This is a standard Java AWT method which typically gets called
  350.      * by the AWT to handle painting this component. It paints this component
  351.      * using the given graphics context. The graphics context clipping region
  352.      * is set to the bounding rectangle of this component and its [0,0]
  353.      * coordinate is this component's top-left corner.
  354.      *
  355.      * @param g the graphics context used for painting
  356.      * @see java.awt.Component#repaint
  357.      * @see java.awt.Component#update
  358.      */
  359.     public void paint(Graphics g)
  360.     {
  361.         super.paint(g);
  362.  
  363.         Dimension dim = size();
  364.         if (image != null)
  365.         {
  366.  
  367.             int imageWidth = image.getWidth(this);
  368.             int imageHeight = image.getHeight(this);
  369.  
  370.             switch(imageStyle)
  371.             {
  372.                 case IMAGE_CENTERED:
  373.                 default:
  374.                 {
  375.                     g.drawImage
  376.                         (image,
  377.                          (dim.width - imageWidth) / 2,
  378.                          (dim.height - imageHeight) / 2,
  379.                          imageWidth,
  380.                          imageHeight,
  381.                          this);
  382.  
  383.                     break;
  384.                 }
  385.  
  386.                 case IMAGE_TILED:
  387.                 {
  388.                     //Calculate number of images that should be drawn horizontally
  389.                     int numHImages = dim.width / imageWidth;
  390.  
  391.                     //Don't forget remainders
  392.                     if (dim.width % imageWidth != 0)
  393.                         numHImages++;
  394.  
  395.                     //Calculate number of images that should be drawn vertically
  396.                     int numVImages = dim.height / imageHeight;
  397.  
  398.                     //Don't forget remainders
  399.                     if (dim.height % imageHeight != 0)
  400.                         numVImages++;
  401.  
  402.                     int h;
  403.                     int v = 0;
  404.                     for (int vCount = 0;vCount < numVImages;vCount++)
  405.                     {
  406.                         h = 0;
  407.                         for (int hCount = 0;hCount < numHImages;hCount++)
  408.                         {
  409.                             g.drawImage(image, h, v, imageWidth, imageHeight, this);
  410.  
  411.                             //Increment to next column
  412.                             h += imageWidth;
  413.                         }
  414.  
  415.                         //Increment to next row
  416.                         v += imageHeight;
  417.                     }
  418.  
  419.                     break;
  420.                 }
  421.  
  422.  
  423.                 case IMAGE_SCALED_TO_FIT:
  424.                 {
  425.                     g.drawImage(image, 0, 0, dim.width, dim.height, this);
  426.  
  427.                     break;
  428.                 }
  429.  
  430.                 case IMAGE_NORMAL:
  431.                 {
  432.                     g.drawImage(image, 0, 0, this);
  433.                     break;
  434.                 }
  435.             }//switch
  436.         }
  437.         else
  438.         {
  439.             g.clearRect(0, 0, dim.width, dim.height);
  440.         }
  441.     }
  442.  
  443.     /**
  444.      * Returns the recommended dimensions to properly display this component.
  445.      * This is a standard Java AWT method which gets called to determine
  446.      * the recommended size of this component.
  447.      *
  448.      * @return  If no image has been loaded, a dimension of 10 by 10 is returned.
  449.      *          If an image has been loaded, the height and width of the image
  450.      *          is returned.
  451.      *
  452.      * @see #minimumSize
  453.      */
  454.     public Dimension preferredSize()
  455.     {
  456.         if (image != null)
  457.             return (new Dimension(image.getWidth(this), image.getHeight(this)));
  458.         else
  459.             return new Dimension(10, 10);
  460.     }
  461.  
  462.  
  463.     /**
  464.      * Returns the minimum dimensions to properly display this component.
  465.      * This is a standard Java AWT method which gets called to determine
  466.      * the minimum size of this component.
  467.      *
  468.      * @return  If no image has been loaded, a dimension of 10 by 10 is returned.
  469.      *          If an image has been loaded, the height and width of the image
  470.      *          is returned.
  471.      * @see #preferredSize
  472.      */
  473.     public Dimension minimumSize()
  474.     {
  475.         return preferredSize();
  476.     }
  477.  
  478.     /**
  479.      * Adds a listener for all event changes.
  480.      * @param PropertyChangeListener listener the listener to add.
  481.      * @see #removePropertyChangeListener
  482.      */
  483.     public void addPropertyChangeListener(PropertyChangeListener listener)
  484.     {
  485.         //super.addPropertyChangeListener(listener);
  486.         changes.addPropertyChangeListener(listener);
  487.     }
  488.  
  489.     /**
  490.      * Removes a listener for all event changes.
  491.      * @param PropertyChangeListener listener the listener to remove.
  492.      * @see #addPropertyChangeListener
  493.      */
  494.     public void removePropertyChangeListener(PropertyChangeListener listener)
  495.     {
  496.         //super.removePropertyChangeListener(listener);
  497.         changes.removePropertyChangeListener(listener);
  498.     }
  499.  
  500.     /**
  501.      * Adds a vetoable listener for all event changes.
  502.      * @param VetoableChangeListener listener the listener to add.
  503.      * @see #removeVetoableChangeListener
  504.      */
  505.     public void addVetoableChangeListener(VetoableChangeListener listener)
  506.     {
  507.          //super.addVetoableChangeListener(listener);
  508.         vetos.addVetoableChangeListener(listener);
  509.     }
  510.  
  511.     /**
  512.      * Removes a vetoable listener for all event changes.
  513.      * @param VetoableChangeListener listener the listener to remove.
  514.      * @see #addVetoableChangeListener
  515.      */
  516.     public void removeVetoableChangeListener(VetoableChangeListener listener)
  517.     {
  518.         //super.removeVetoableChangeListener(listener);
  519.         vetos.removeVetoableChangeListener(listener);
  520.     }
  521.  
  522.     private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
  523.         stream.defaultReadObject();
  524.  
  525.         errors = ResourceBundle.getBundle("symantec.itools.resources.ErrorsBundle");
  526.  
  527.         if (url != null) {
  528.             image = getToolkit().getImage(url);
  529.  
  530.             MediaTracker tracker = new MediaTracker(this);
  531.             tracker.addImage(image, 0);
  532.             try {
  533.                 tracker.waitForAll();
  534.             }
  535.             catch(InterruptedException e) {
  536.             Object[] args = { url };
  537.             throw new IOException(MessageFormat.format(errors.getString("ErrorLoadingImageForURL"), args));
  538.             }
  539.         }
  540.     }
  541.  
  542.    /**
  543.      * Image that this viewer is displaying.
  544.      */
  545.     transient protected Image image;
  546.  
  547.     /**
  548.      * Name of file, if any, associated with this image.
  549.      */
  550.     protected String fileName;
  551.  
  552.     /**
  553.      * URL of the image being displayed.
  554.      */
  555.     protected URL url;
  556.  
  557.     /**
  558.      * Determines how to draw the image.
  559.      */
  560.     protected int imageStyle;
  561.  
  562.     /**
  563.      * Error strings.
  564.      */
  565.     transient protected ResourceBundle errors;
  566.  
  567.     private symantec.itools.beans.VetoableChangeSupport vetos   = new symantec.itools.beans.VetoableChangeSupport(this);
  568.     private symantec.itools.beans.PropertyChangeSupport changes = new symantec.itools.beans.PropertyChangeSupport(this);
  569.  
  570. }
  571.